import { get, post } from "@/utils/http";

// API 常量
export const Api = {
  GoodCategories: "/good/categories",
  CheckGoodId: "/good/checkGood",
  GoodPage: "/good/page",
  GoodEnable: "/good",
  GoodAdd: "/good",
  ProductAdd: "/product",
  ProductUpdate: "/product/:id/update",
  ProductEnable: "/product/:id/enable",
  ProductPage: "/product/page",
  GoodUpdate: "/good/:id/update",
} as const;

// 物品分类数据类型
export interface GoodsCategoryRecord {
  code: number;
  key: string;
  desc: string;
  name: string;
  folder: string;
}

// 物品名称多语言类型
export interface GoodName {
  zh: string;
  en: string;
  ar: string;
  text: string;
}

// 物品信息数据类型
export interface GoodInfo {
  id: string;
  createUser: string | null;
  createTime: string;
  modifyUser: string | null;
  modifyTime: string;
  name: GoodName;
  previewImageUrl: string;
  animationImageUrl: string;
  remark: string;
  category: string;
  subCategory: string;
  enable: boolean;
}

// 物品搜索请求参数类型
export interface GoodSearchReq {
  id?: string;
  pageNum?: number;
  pageSize?: number;
  category?: string;
  enable?: boolean;
}

// 物品分页响应数据类型
export interface GoodPageResponse {
  hasNextPage: boolean;
  list: GoodInfo[];
  nextPage: number;
  count: number;
  total: number;
  size: number;
}

// 物品上下架状态更新请求参数类型
export interface GoodUpdateEnableReq {
  enable: boolean;
}

// 物品新增/编辑请求参数类型
export interface GoodsDto {
  id?: string;
  name: GoodName;
  previewImageUrl: string;
  animationImageUrl: string;
  remark: string;
  category: string;
  subCategory: string;
  enable: boolean;
}

// 商品
export interface PruductDto {
  id?: string;
  goodsId: string;
  durationOfDay: number;
  platformCurrencyType: string;
  salePrice: number;
  unitPrice: number;
  enable: boolean;
  remark: string;
  goods: GoodsDto;
  category: string;
}

// 商品分页响应数据类型
export interface ProductPageResponse {
  hasNextPage: boolean;
  list: PruductDto[];
  nextPage: number;
  count: number;
  total: number;
  size: number;
}


// 获取物品分类
export function getGoodCategoriesApi(): Promise<{
  code: number;
  data: GoodsCategoryRecord[];
  message?: string;
}> {
  return get(Api.GoodCategories);
}

// 校验物品ID是否存在
export function checkGoodIdApi(goodId: string): Promise<{
  code: number;
  data: GoodInfo;
  message?: string;
}> {
  return get(`${Api.CheckGoodId}/${goodId}`);
}

// 获取物品分页列表
export function getGoodPageApi(params: GoodSearchReq): Promise<{
  code: number;
  data: GoodPageResponse;
  message?: string;
}> {
  return post(Api.GoodPage, params);
}

// 更新物品上下架状态
export function updateGoodEnableApi(
  goodId: string,
  params: GoodUpdateEnableReq
): Promise<{
  code: number;
  data: null;
  message?: string;
}> {
  return post(`${Api.GoodEnable}/${goodId}/enable`, params);
}

// 新增物品
export function addGoodApi(params: GoodsDto): Promise<{
  code: number;
  data: null;
  message?: string;
}> {
  return post(Api.GoodAdd, params);
}
// 更新物品
export function updateGoodApi(params: GoodsDto): Promise<{
  code: number;
  data: null;
  message?: string;
}> {
  return post(Api.GoodUpdate.replace(":id", String(params.id)), params);
}

// 获取商品分页列表
export function getProductPageApi(params: GoodSearchReq): Promise<{
  code: number;
  data: ProductPageResponse;
  message?: string;
}> {
  return post(Api.ProductPage, params);
}

// 新增商品
export function addProductApi(params: PruductDto): Promise<{
  code: number;
  data: null;
  message?: string;
}> {
  return post(Api.ProductAdd, params);
}
// 更新商品
export function updateProductApi(params: PruductDto): Promise<{
  code: number;
  data: null;
  message?: string;
}> {
  return post(Api.ProductUpdate.replace(":id", String(params.id)), params);
}

// 更新商品上下架状态
export function updateProductEnableApi(
  productId: string,
  params: GoodUpdateEnableReq
): Promise<{
  code: number;
  data: null;
  message?: string;
}> {
  return post(Api.ProductEnable.replace(":id", String(productId)), params);
}
